<?php
/**
 * PDF reporting library
 *
 * This library contains all methods needed to output
 * a PDF detailed report.
 * @package Reports
 */

/**
 *
 */
require_once(AD_SERVER_PATH . '/pkgs/init.php');
require_once(AD_SERVER_PATH . '/pkgs/pdf.php');
require_once(AD_SERVER_PATH . '/reports/pdf_report.php');

define('MAX_IMG_WIDTH', 55);
define('MAX_IMG_HEIGHT', 40);

/**
 * PDF reporting
 *
 * This library contains all methods needed to output
 * a PDF detailed report.
 * @package Reporting
 */
class CPDF_detail extends CPDF_report
{
	/**
	 * Constructor
	 *
	 * Initilizes class variables containing
	 * path to PDF template and a FPDF object.
	 */
	public function __construct()
	{
		$this->pdf = new CPDF();
	}

	/**
	 * Get PDF detail report
	 *
	 * Output PDF detail report.
	 */
	public function get_report()
	{
		$xml = simplexml_load_string($this->get_data());

		foreach ($xml->rows->row AS $row)
		{
			$this->pdf->set_left_header_text($this->get_address($row));
			$this->pdf->set_right_header_text('MLS: ' . $row->ml_number);
			$this->pdf->AddPage();

			$this->set_cords(20, 25);
			foreach ($row AS $key => $val)
			{
				$this->pdf->Cell(50, 5, ucfirst($key), 'T');
				$this->pdf->MultiCell(126, 5, $val, 'T');
				$this->set_cords(20, '');
			}
			$this->_print_img_grid($row->ml_number, $row->picture_count, 20, 25, 5);
		}
		$this->pdf->Output('AD Detail Report.pdf', 'i');
	}

	/**
	 * Print image grid
	 *
	 * Outputs images, layed out in a grid, to PDF page.
	 * @param int $ml_number
	 * @param int $img_count
	 * @param int $x_offset
	 * @param int $y_offset
	 * @param int $padding
	 */
	private function _print_img_grid($ml_number, $img_count, $x_offset, $y_offset, $padding)
	{
		for ($i = 0; $i <= $img_count; $i++)
		{
			$col = $i % 3;
			$row = floor($i / 3) % 5;
			$x = $x_offset + ($col * (MAX_IMG_WIDTH  + $padding));
			$y = $y_offset + ($row * (MAX_IMG_HEIGHT + $padding));

			if ($col == 0 && $row == 0)
			{
				$this->pdf->AddPage();
			}
			$this->pdf->Image('', $x, $y, MAX_IMG_WIDTH, MAX_IMG_HEIGHT, '', '', $ml_number, $i);
		}
	}
}
